package com.mqstudy.rabbitmq.producer;

import com.mqstudy.common.model.Message;
import com.mqstudy.rabbitmq.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * RabbitMQ生产者
 */
@Slf4j
@Component
public class RabbitMQProducer {
    
    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    @Autowired
    private MessageConverter messageConverter;
    
    /**
     * 发送直连消息
     */
    public void sendDirectMessage(Message message) {
        log.info("发送直连消息: {}", message);
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.DIRECT_EXCHANGE,
                RabbitMQConfig.DIRECT_ROUTING_KEY,
                message
        );
    }
    
    /**
     * 发送扇出消息
     */
    public void sendFanoutMessage(Message message) {
        log.info("发送扇出消息: {}", message);
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.FANOUT_EXCHANGE,
                "", // 扇出交换机不需要路由键
                message
        );
    }
    
    /**
     * 发送主题消息1
     */
    public void sendTopicMessage1(Message message) {
        log.info("发送主题消息1: {}", message);
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.TOPIC_EXCHANGE,
                RabbitMQConfig.TOPIC_ROUTING_KEY_1,
                message
        );
    }
    
    /**
     * 发送主题消息2
     */
    public void sendTopicMessage2(Message message) {
        log.info("发送主题消息2: {}", message);
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.TOPIC_EXCHANGE,
                RabbitMQConfig.TOPIC_ROUTING_KEY_2,
                message
        );
    }
    
    /**
     * 发送延迟消息
     */
    public void sendDelayMessage(Message message, int delaySeconds) {
        log.info("发送延迟消息: {}, 延迟{}秒", message, delaySeconds);
        
        MessagePostProcessor messagePostProcessor = msg -> {
            MessageProperties properties = msg.getMessageProperties();
            properties.setExpiration(String.valueOf(delaySeconds * 1000));
            return msg;
        };
        
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.DELAY_EXCHANGE,
                "delay.routing.key",
                message,
                messagePostProcessor
        );
    }
    
    /**
     * 发送带优先级的消息
     */
    public void sendPriorityMessage(Message message, int priority) {
        log.info("发送优先级消息: {}, 优先级: {}", message, priority);
        
        MessagePostProcessor messagePostProcessor = msg -> {
            MessageProperties properties = msg.getMessageProperties();
            properties.setPriority(priority);
            return msg;
        };
        
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.DIRECT_EXCHANGE,
                RabbitMQConfig.DIRECT_ROUTING_KEY,
                message,
                messagePostProcessor
        );
    }
    
    /**
     * 发送批量消息
     */
    public void sendBatchMessages(String topic, int count) {
        log.info("发送批量消息到主题: {}, 数量: {}", topic, count);
        
        for (int i = 1; i <= count; i++) {
            Message message = Message.builder()
                    .messageId("batch-" + i)
                    .topic(topic)
                    .content("批量消息内容 " + i)
                    .messageType("BATCH")
                    .build();
            
            rabbitTemplate.convertAndSend(
                    RabbitMQConfig.DIRECT_EXCHANGE,
                    RabbitMQConfig.DIRECT_ROUTING_KEY,
                    message
            );
        }
    }
} 